package com.itheima.lawyer.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.lawyer.common.R;
import com.itheima.lawyer.entity.Lawyer;
import com.itheima.lawyer.service.LawyerService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

/**
 * 律师管理
 * @author 86134
 */
@RestController
@RequestMapping("/lawyer")
@Slf4j
public class LawyerController {
    @Autowired
    private LawyerService lawyerService;


    /**
     * 新增律师
     * @param lawyer
     * @return
     */
    @PostMapping
    public R<String> save(@RequestBody Lawyer lawyer){

        lawyerService.save(lawyer);

        return R.success("新增律师成功");
    }

    /**
     * 律师信息分页查询
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public R<Page> page(int page,int pageSize,String name){
        System.out.println(name);

        //构造分页构造器对象
        Page<Lawyer> pageInfo = new Page<>(page,pageSize);

        //条件构造器
        LambdaQueryWrapper<Lawyer> queryWrapper = new LambdaQueryWrapper<>();
        //添加过滤条件
        queryWrapper.like(name != null,Lawyer::getName,name);


        //执行分页查询
        lawyerService.page(pageInfo,queryWrapper);


        return R.success(pageInfo);
    }

    /**
     * 根据id查询律师信息
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R<Lawyer> get(@PathVariable Long id){

        Lawyer lawyer = lawyerService.getById(id);

        return R.success(lawyer);
    }

    /**
     * 修改律师
     * @param lawyer
     * @return
     */
    @PutMapping
    public R<String> update(@RequestBody Lawyer lawyer){

        lawyerService.updateById(lawyer);

        return R.success("修改律师成功");
    }

    /**
     * 根据id删除律师
     * @param ids
     * @return
     */
    @DeleteMapping
    public R<String> delete(@RequestParam List<Long> ids){
        log.info("删除律师，id为：{}",ids);

        lawyerService.removeByIds(ids);

        return R.success("律师删除成功");
    }


}
